/**
 * Created by brain on 2015/10/18.
 */

var WildUtils = {

    match: function (string, pattern) {
        var expression = this.globToRegex(pattern);
        // console.log(pattern, "reg:", expression);
        var regExp = new RegExp(expression);
        return regExp.test(string);
    },

    globToRegex: function (glob) {
        var specialChars = "\\^$*+?.()|{}[]-";
        var regexChars = ["^"];
        for (var i = 0; i < glob.length; ++i) {
            var c = glob.charAt(i);
            switch (c) {
                case '?':
                    regexChars.push(".");
                    break;
                case '*':
                    regexChars.push(".*");
                    break;
                default:
                    if (specialChars.indexOf(c) >= 0) {
                        regexChars.push("\\");
                    }
                    regexChars.push(c);
            }
        }
        regexChars.push("$");
        var string = regexChars.join("");
        return new RegExp(string);
    },
};